# coding:utf8
import pymysql



class DBUtils(object):

    def connDB(self,host='127.0.0.1',port=3306, user='root',password='123456',database='drug',charset='utf8'):                              #连接数据库，返回链接和游标

        conn=pymysql.connect(host=host,port=port,user=user,passwd=password,db=database,charset=charset);

        cur=conn.cursor();

        return (conn,cur);



    def exeUpdate(self,conn,cur,sql):                #更新或插入操作

        sta=cur.execute(sql);

        conn.commit();

        return (sta);



    def exeDelete(self,conn,cur,IDs):                #删除操作

        sta=0;

        for eachID in IDs.split(' '):

            sta+=cur.execute("delete from students where Id=%d"%(int(eachID)));

        conn.commit();

        return (sta);



    def exeQuery(self,cur,sql):                      #查找操作

        effect_row = cur.execute(sql);
        return (effect_row,cur);



    def connClose(self,conn,cur):                    #关闭连接，释放资源

        cur.close();

        conn.close();



if __name__ == '__main__':

        dbUtil = DBUtils();

        conn,cur = dbUtil.connDB();
        dbUtil.connClose(conn,cur);
